package com.iwhalecloud.bss.kite.cucc.order.operate;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.InstProxyUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.IInst;
import com.iwhalecloud.bss.kite.order.client.api.operate.IOperateOrderInstService;
import com.iwhalecloud.bss.kite.order.client.context.OrderContext;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class OperateOrderInstService implements IOperateOrderInstService {

    @Override
    public Boolean updateFieldValue(String tableName, String instId, String fieldName, String value) {
        IInst inst = InstContainer.ofRequest(OrderContext.getOrderRequest()).getInst(tableName, instId);
        if (Objects.nonNull(inst)) {
            String oldValue = KiteBeanUtils.getValueByName(inst, fieldName);
            KiteBeanUtils.setValueByName(inst, fieldName, value);
            InstProxyUtils.createOrderItemChange(tableName, instId, fieldName, value, oldValue);
        }
        return Boolean.TRUE;
    }
}
